package weaver.interfaces.zhlx.duss.action.salesmodule.c4;

import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.C4Util;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 客户主数据变更流程结束调用回传C4, 更新客户主数据接口
 * @ModifyLog:
 **/
public class WeaKhzsjbglcC4Action extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog(">>>>>WeaKhzsjbglcC4Action>>>>>requestid=" + requestid);
        try {
            String message = "调用C4系统接口返回:";
            RecordSet rs = new RecordSet();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String uuid = Util.null2String(rs.getString("uuid"));
            JSONObject paramJson = new JSONObject();
            paramJson.put("Name", Util.null2String(rs.getString("khmc1")));
            paramJson.put("AdditionalName", Util.null2String(rs.getString("khmc2")));
            paramJson.put("Z_PaymentWays_KUT", Util.null2String(rs.getString("fkfs")));
            paramJson.put("Z_PrePurchaseAmoutContent_KUT", Util.null2String(rs.getString("yjcge")));
            paramJson.put("Z_PrePurchaseAmoutcurrencyCode_KUT", Util.null2String(rs.getString("yqcgehb")));
            paramJson.put("Z_ProxyRate_KUT", Util.null2String(rs.getString("dlfl")));
            String sfyfwss = Util.null2String(rs.getString("sfyfwss"));
            paramJson.put("Z_IsLitigious_KUT", "0".equals(sfyfwss));
            paramJson.put("Z_EnterpriseStatus_KUT", Util.null2String(rs.getString("tycqyzt")));
            String earmgzcpdsfzfkqd = Util.null2String(rs.getString("earmgzcpdsfzfkqd"));
            paramJson.put("Z_EAR_KUT", "0".equals(earmgzcpdsfzfkqd));
            paramJson.put("Z_CITCIC_KUT", Util.null2String(rs.getString("hwkhzxbhdqk")));
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaKhzsjbglcC4Action");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("uuid", uuid);
            JSONObject resultJson = C4Util.updateKhzsj(paramJson, dataMap);
            String code = Util.null2String(resultJson.getString("code"));
            if (!"success".equals(code)) {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
            int counts = rs.getCounts();
            int count = 0;
            while (rs.next()) {
                JSONObject xzdataJson = new JSONObject();
                String mxid = Util.null2String(rs.getString("id"));
                String uuid_dt1 = Util.null2String(rs.getString("uuid"));
                String xszz = Util.null2String(rs.getString("xszz"));
                String fxqdbm = Util.null2String(rs.getString("fxqdbm"));
                String cpzbm = Util.null2String(rs.getString("cpzbm"));
                String xsdqbm = Util.null2String(rs.getString("xsdqbm"));
                String bzbm = Util.null2String(rs.getString("bzbm"));
                String gjmytkbm = Util.null2String(rs.getString("gjmytkbm"));
                String gjmytkdd = Util.null2String(rs.getString("gjmytkdd"));
                String fktjbm = Util.null2String(rs.getString("fktjbm"));
                xzdataJson.put("SalesOrganisationID", xszz);
                xzdataJson.put("DistributionChannelCode", fxqdbm);
                xzdataJson.put("DivisionCode", cpzbm);
                xzdataJson.put("Z_SalesRegion_KUT", xsdqbm);
                xzdataJson.put("CurrencyCode", bzbm);
                xzdataJson.put("IncotermsClassificationCode", gjmytkbm);
                xzdataJson.put("IncotermsLocation", gjmytkdd);
                xzdataJson.put("PaymentTermsCode", fktjbm);
                JSONObject xgdataJson = new JSONObject();
                xgdataJson.put("CurrencyCode", bzbm);
                xgdataJson.put("IncotermsClassificationCode", gjmytkbm);
                xgdataJson.put("IncotermsLocation", gjmytkdd);
                xgdataJson.put("PaymentTermsCode", fktjbm);
                xgdataJson.put("Z_SalesRegion_KUT", xsdqbm);
                if ("".equals(uuid_dt1)) {
                    JSONObject resultJson1 = C4Util.createXssj(xzdataJson, dataMap);
                    writeLog(">>>>>WeaKhzsjbglcC4Action>>>>>resultJson1="+resultJson1);
                    String code1 = Util.null2String(resultJson1.getString("code"));
                    if ("success".equals(code1)) {
                        String data = Util.null2String(resultJson1.getString("data"));
                        rs.executeUpdate("update "+tablename+"_dt1 set uuid=? where id=?",data,mxid);
                        count++;
                    } else {
                        message += Util.null2String(resultJson1.getString("message"));
                    }
                } else {
                    Map<String, Object> dataMap1 = new HashMap<>();
                    dataMap1.put("jkdymc", "WeaKhzsjbglcC4Action");
                    dataMap1.put("lcid", requestid);
                    dataMap1.put("dylc", requestid);
                    dataMap1.put("uuid", uuid_dt1);
                    JSONObject resultJson2 = C4Util.updateXssj(xgdataJson, dataMap1);
                    String code2 = Util.null2String(resultJson2.getString("code"));
                    if ("success".equals(code2)) {
                        count++;
                    } else {
                        message += Util.null2String(resultJson2.getString("message"));
                    }
                }
            }
            if (counts != count) {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaKhzsjbglcC4Action>>>>>Exception="+e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}